﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NWN.GDY.Service.Entity;
using Sdo.Payment.Common;
using NWN.GDY.Service.Common;

namespace NWN.GDY.Service.Core.HandcardCreator
{
    public class StraightCreator : AbstractHDCreator
    {
        AbstractHDCreator hdcreator;
        string description = "顺子";
        public override string GetDescription()
        {
            if (hdcreator != null)
                return hdcreator.GetDescription() + "_" + description;
            return description;
        }
        public StraightCreator()
        {
        }

        public StraightCreator(AbstractHDCreator creator)
        {
            this.hdcreator = creator;
        }

        public override void CreateHandCard(List<Card> cards, List<Card> AllCards, List<HandCard> handcards)
        {
            if (hdcreator != null)
            {
                hdcreator.CreateHandCard(cards, AllCards, handcards);
            }
            if (cards.Count < 3)
                return;
            //如最小牌都是王，则肯定是炸，直接返回
            if (cards[0].CardType > 4)
                return;
            List<Card> acards = (from c in cards where c.CardNum == 14 select c).ToList();
            int ACount = acards.Count();
            if (ACount > 1)
                return;
            if (ACount == 1)
            {
                List<Card> newcards = new List<Card>();
                Card createAcard = (from cc in AllCards where cc.CardName == acards[0].CardName && cc.CardNum == 1 select cc).FirstOrDefault();
                if (createAcard == null)
                    throw new Exception("转换A牌异常");
                newcards.Add(createAcard);
                foreach (Card c in cards)
                {
                    if (c.CardNum != 14)
                    {
                        newcards.Add(c);
                    }
                }
                CreateHandCardlv2(newcards, AllCards, handcards);
            }
            CreateHandCardlv2(cards, AllCards, handcards);
        }

        void CreateHandCardlv2(List<Card> cards, List<Card> AllCards, List<HandCard> handcards)
        {
            Hashtable ht = new Hashtable();
            int mincardnum = cards[0].CardNum;
            Card tempcard = null;
            List<int> needjokers = new List<int>();
            foreach (Card c in cards)
            {
                //循环到王退出
                if (c.CardType == (int)CardType.joker || c.CardType == (int)CardType.sjoker)
                    break;
                if (ht.Contains(c.CardNum))
                    return;
                if (c.CardType != (int)CardType.sjoker && c.CardType != (int)CardType.joker)
                    ht.Add(c.CardNum, c.CardNum);
                if (c.CardNum > mincardnum + cards.Count - 1)
                    return;
                if (tempcard != null)
                {
                    if (c.CardNum != tempcard.CardNum + 1)
                    {
                        for (int i = 0; i < c.CardNum - tempcard.CardNum - 1; i++)
                        {
                            //缺牌，需王补
                            needjokers.Add(tempcard.CardNum + i + 1);
                        }
                    }
                }
                tempcard = c;
            }
            int jokercount = (from c in cards where c.CardType > 4 && c.CardType <= 6 select c).Count();
            if (needjokers.Count == 0 && jokercount == 0)
            {
                //Log.Lw("顺子" + cards[0].SimpleName);
                handcards.Add(new HandCard() { Cards = cards, Type = HandCardType.Straight });
            }
            else
            {
                List<Card> addjokercards = (from c in AllCards where (needjokers.Contains(c.CardNum) && c.CardType == (int)CardType.jokeraltern) select c).ToList();
                if (addjokercards == null)
                    addjokercards = new List<Card>();
                foreach (Card c in cards)
                {
                    if (c.CardType != (int)CardType.joker && c.CardType != (int)CardType.sjoker)
                    {
                        addjokercards.Add(c);
                    }
                }
                List<Card> addjokercardssorted = (from c in addjokercards orderby c.CardNum,c.CardType select c).ToList();
                if (jokercount == needjokers.Count)
                {
                    //Log.Lw("嵌王顺子" + addjokercardssorted[0].SimpleName);
                    handcards.Add(new HandCard() { Cards = addjokercardssorted, Type = HandCardType.Straight });
                }
                else
                {
                    for (int i = 0; i < jokercount - needjokers.Count+1; i++)
                    {
                        if (addjokercardssorted[0].CardNum - (jokercount - needjokers.Count) + i >= 1 && addjokercardssorted[addjokercardssorted.Count - 1].CardNum + i <= 14)
                        {
                            List<Card> tempcards = (from c in AllCards where ((c.CardNum >= (addjokercardssorted[0].CardNum - (jokercount - needjokers.Count) + i) && c.CardNum < addjokercardssorted[0].CardNum) || (c.CardNum > addjokercardssorted[addjokercardssorted.Count - 1].CardNum && c.CardNum <= (addjokercardssorted[addjokercardssorted.Count - 1].CardNum + i))) && c.CardType == 10 select c).ToList();
                            List<Card> tempcardsall = new List<Card>();
                            foreach (Card c in addjokercardssorted)
                            {
                                tempcardsall.Add(c);
                            }
                            foreach (Card c in tempcards)
                            {
                                tempcardsall.Add(c);
                            }
                            List<Card> tempcardssorted = (from c in tempcardsall orderby c.CardNum,c.CardType select c).ToList();
                            //Log.Lw("前后王顺子" + tempcardssorted[0].SimpleName);
                            handcards.Add(new HandCard() { Cards = tempcardssorted, Type = HandCardType.Straight });
                        }
                    }
                }
            }
        }

        public override int CompareHandCard(HandCard lasthc, HandCard currenthc)
        {
            if (hdcreator != null)
            {
                if (hdcreator.CompareHandCard(lasthc, currenthc) == GDYConsts.ReturnDefine.SUCCESS)
                    return GDYConsts.ReturnDefine.SUCCESS;
            }
            if (lasthc.Type != HandCardType.Straight || currenthc.Type != HandCardType.Straight)
                return GDYConsts.ReturnDefine.HANDCARDTYPEERROR;
            if (lasthc.Cards.Count == currenthc.Cards.Count && lasthc.Cards[0].CardNum + 1 == currenthc.Cards[0].CardNum)
                return GDYConsts.ReturnDefine.SUCCESS;
            return GDYConsts.ReturnDefine.HANDCARDCOMPAREWRONG;
        }
    }
}